// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace MatrixFramework.Photos.Domain.Entities;

/// <summary>
/// 人物实体
/// </summary>
public class Person : FullAuditedAggregateRoot<Guid>, IMultiTenant
{
    /// <summary>
    /// 租户ID
    /// </summary>
    public virtual Guid? TenantId { get; protected set; }

    /// <summary>
    /// 人物姓名
    /// </summary>
    [Required]
    [MaxLength(100)]
    public virtual string Name { get; protected set; } = string.Empty;

    /// <summary>
    /// 人物昵称
    /// </summary>
    [MaxLength(100)]
    public virtual string? Nickname { get; protected set; }

    /// <summary>
    /// 人物描述
    /// </summary>
    [MaxLength(500)]
    public virtual string? Description { get; protected set; }

    /// <summary>
    /// 人物类型
    /// </summary>
    public virtual PersonType PersonType { get; protected set; }

    /// <summary>
    /// 头像URL
    /// </summary>
    [MaxLength(2000)]
    public virtual string? AvatarUrl { get; protected set; }

    /// <summary>
    /// 人脸特征向量（用于人脸识别）
    /// </summary>
    public virtual byte[]? FaceEncoding { get; protected set; }

    /// <summary>
    /// 年龄范围开始
    /// </summary>
    public virtual int? AgeRangeStart { get; protected set; }

    /// <summary>
    /// 年龄范围结束
    /// </summary>
    public virtual int? AgeRangeEnd { get; protected set; }

    /// <summary>
    /// 性别
    /// </summary>
    [MaxLength(10)]
    public virtual string? Gender { get; protected set; }

    /// <summary>
    /// 关系类型
    /// </summary>
    [MaxLength(50)]
    public virtual string? Relationship { get; protected set; }

    /// <summary>
    /// 是否为家庭重要成员
    /// </summary>
    public virtual bool IsImportant { get; protected set; }

    /// <summary>
    /// 首次出现时间
    /// </summary>
    public virtual DateTime? FirstAppearance { get; protected set; }

    /// <summary>
    /// 最后出现时间
    /// </summary>
    public virtual DateTime? LastAppearance { get; protected set; }

    /// <summary>
    /// 照片关联
    /// </summary>
    public virtual ICollection<PhotoPerson> PhotoPersons { get; protected set; } = new List<PhotoPerson>();

    #region 构造函数

    /// <summary>
    /// 参数化构造函数
    /// </summary>
    /// <param name="id">人物ID</param>
    /// <param name="tenantId">租户ID</param>
    /// <param name="name">姓名</param>
    /// <param name="personType">人物类型</param>
    public Person(
        Guid id,
        Guid? tenantId,
        string name,
        PersonType personType = PersonType.Unknown) : base(id)
    {
        TenantId = tenantId;
        Name = name;
        PersonType = personType;
        IsImportant = false;
    }

    /// <summary>
    /// 无参构造函数（用于EF Core）
    /// </summary>
    protected Person()
    {
        PhotoPersons = new List<PhotoPerson>();
    }

    #endregion

    #region 业务方法

    /// <summary>
    /// 更新基本信息
    /// </summary>
    /// <param name="name">姓名</param>
    /// <param name="nickname">昵称</param>
    /// <param name="description">描述</param>
    /// <param name="relationship">关系</param>
    public virtual void UpdateBasicInfo(string name, string? nickname, string? description, string? relationship)
    {
        Name = name;
        Nickname = nickname;
        Description = description;
        Relationship = relationship;
    }

    /// <summary>
    /// 设置人物类型
    /// </summary>
    /// <param name="personType">人物类型</param>
    public virtual void SetPersonType(PersonType personType)
    {
        PersonType = personType;
    }

    /// <summary>
    /// 设置头像
    /// </summary>
    /// <param name="avatarUrl">头像URL</param>
    public virtual void SetAvatar(string avatarUrl)
    {
        AvatarUrl = avatarUrl;
    }

    /// <summary>
    /// 设置人脸特征
    /// </summary>
    /// <param name="faceEncoding">人脸特征向量</param>
    public virtual void SetFaceEncoding(byte[] faceEncoding)
    {
        FaceEncoding = faceEncoding;
    }

    /// <summary>
    /// 设置年龄范围
    /// </summary>
    /// <param name="ageRangeStart">开始年龄</param>
    /// <param name="ageRangeEnd">结束年龄</param>
    public virtual void SetAgeRange(int? ageRangeStart, int? ageRangeEnd)
    {
        AgeRangeStart = ageRangeStart;
        AgeRangeEnd = ageRangeEnd;
    }

    /// <summary>
    /// 设置性别
    /// </summary>
    /// <param name="gender">性别</param>
    public virtual void SetGender(string gender)
    {
        Gender = gender;
    }

    /// <summary>
    /// 设置重要性
    /// </summary>
    /// <param name="isImportant">是否重要</param>
    public virtual void SetImportance(bool isImportant)
    {
        IsImportant = isImportant;
    }

    /// <summary>
    /// 更新出现时间
    /// </summary>
    /// <param name="appearanceTime">出现时间</param>
    public virtual void UpdateAppearanceTime(DateTime appearanceTime)
    {
        if (!FirstAppearance.HasValue || appearanceTime < FirstAppearance)
        {
            FirstAppearance = appearanceTime;
        }

        if (!LastAppearance.HasValue || appearanceTime > LastAppearance)
        {
            LastAppearance = appearanceTime;
        }
    }

    #endregion
}

/// <summary>
/// 人物类型枚举
/// </summary>
public enum PersonType
{
    /// <summary>
    /// 未知
    /// </summary>
    Unknown = 0,

    /// <summary>
    /// 家庭成员
    /// </summary>
    Family = 1,

    /// <summary>
    /// 朋友
    /// </summary>
    Friend = 2,

    /// <summary>
    /// 同事
    /// </summary>
    Colleague = 3,

    /// <summary>
    /// 同学
    /// </summary>
    Classmate = 4,

    /// <summary>
    /// 陌生人
    /// </summary>
    Stranger = 5,

    /// <summary>
    /// 公众人物
    /// </summary>
    PublicFigure = 6,

    /// <summary>
    /// 自己
    /// </summary>
    Self = 7
}